package com.ufc.sst.bean;

import java.awt.event.ActionEvent;


import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import com.ufc.sst.model.Usuario;

@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean {
	private Usuario usuario;

	public String login(ActionEvent actionEvent) {
		System.out.println("entrou no login");
		RequestContext context = RequestContext.getCurrentInstance();
		FacesMessage msg = null;
		boolean loggedIn = false;

		if (usuario.getEmail() != null && usuario.getEmail().equals("admin")
				&& usuario.getSenha() != null && usuario.getSenha().equals("admin")) {
			loggedIn = true;
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome",
					usuario.getNome());
		} else {
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error",
					"Invalid credentials");
		}

		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);
		return "cadastroUsuario";
	}

	// Getters and Setters
	public Usuario getAluno() {
		return usuario;
	}

	public void setAluno(Usuario usuario) {
		this.usuario = usuario;
	}
}
